<!--  -->
<template>
  <div class="Login_container">
    <el-row>
      <el-col :span="12" :offset="0" :xs="0"></el-col>
      <el-col :span="12" :offset="0" :xs="24">
        <el-form class="login_from" :model="loginForm" :rules="rules" ref="loginForms">
          <h1 class="hello">hello</h1>
          <p class="welcome">欢迎来到coco甄选</p>
          <p>hhhhhhhhhhhh</p>
          <el-form-item label="" prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input :prefix-icon="Lock" v-model="loginForm.password" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="login_btn" :loading="loading" type="primary" @click="login">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
// 引入用户相关的小仓库
import useUserStore from '@/store/modulles/user'
import { ElNotification } from 'element-plus'
// 引入路由
import { useRouter } from 'vue-router'
// 引入时间函数
import { getTime } from '@/utils/time'
//
import { userLogin } from '@/store/modulles/user'
let loginForm = reactive({ username: 'admin', password: '111111' })
let useStore = useUserStore()
let loading = ref(false)
let $router = useRouter()
let loginForms = ref()
const login = async () => {
  await loginForms.value.validate()
  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    //  登录成功跳转到首页
    $router.push('/')
    //  登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: 'Hi,' + getTime() + '好',
    })
    // 登录成功关闭loading
    loading.value = false
  } catch (error) {
    // 登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}
// 自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('账号长度至少六位'))
  }
}
//定义校验规则
const rules = {
  // username: [{ required: true, min: 3, max: 10, message: '字符长度在3-10之间', trigger: 'blur' }],
  // 自定义校验规则
  username: [{ trigger: 'change', validator: validatorUserName }],
  // password: [{ required: true, min: 6, max: 10, message: '字符长度在6-10之间', trigger: 'blur' }]
  password: [{ trigger: 'change', validator: validatorPassword }],
}
</script>

<style scoped lang="scss">
.Login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_from {
    position: relative;
    top: 100px;
    width: 80%;
    // height: 400px;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 30px;

    .hello {
      color: #fff;
      font-size: 40px;
    }

    .welcome {
      margin: 20px 0;
      color: #fff;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
